/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cholm.media.castdr;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;


import com.google.sample.castcompanionlibrary.cast.VideoCastManager;

import static org.cholm.media.castdr.LogUtils.LOGd;

/**
 * Created by cholm on 4/3/14.
 */
public class Application extends android.app.Application {
    private static final String TAG = LogUtils.makeLogTag(Application.class);
    private static VideoCastManager mCastMgr = null;

    /*
	 * (non-Javadoc)
	 *
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
        LOGd(TAG, "Created application");
    }

    /**
     * Get the cast manager
     *
     * @param context Context
     *
     * @return The static instance of the cast manager
     */
    public static VideoCastManager getCastManager(Context context) {

        if (null == mCastMgr) {
            final String appId = Settings.getApplicationId(context);
            mCastMgr = VideoCastManager.initialize(context, appId,
                    null, null);
            mCastMgr.enableFeatures(VideoCastManager.FEATURE_NOTIFICATION
                    | VideoCastManager.FEATURE_LOCKSCREEN
                    | VideoCastManager.FEATURE_DEBUGGING);

        }
        mCastMgr.setContext(context);
        mCastMgr.setStopOnDisconnect(Settings.isDestroyAppOnDisconnect
                (context));
        return mCastMgr;
    }

    /**
     * Force restart ourselves
     */
    public static void restartApp(Context ctx) {
        Intent intent = new Intent(ctx, Activity.class);
        int pendingIntentId = 123456;
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx,
                pendingIntentId,    intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager)ctx.getSystemService(Context
                .ALARM_SERVICE);
        // Restart in 100ms
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100,
                pendingIntent);
        // Kill ourselves
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
/*
 * EOF
 */
